﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Journal.Common.Models;
using Journal.Common.Objects.Enums;
using Journal.Domain.IDomainServices;
using Journal.Domain.Models;
using Journal.Front.Presentation.Mappers;
using Journal.Front.Presentation.Models.StudentModels;
using Journal.Front.Presentation.PresentationServicesInterfaces;

namespace Journal.Front.Presentation.PresentationServices
{
    public class StudentPresentationService : IStudentPresentationService
    {
        private readonly IStudyGroupDomainService studyGroupDomainService;
        private readonly IUserDomainService userDomainService;
        private readonly IRoleDomainService roleDomainService;

        public StudentPresentationService(IStudyGroupDomainService studyGroupDomainService, IUserDomainService userDomainService, IRoleDomainService roleDomainService)
        {
            this.studyGroupDomainService = studyGroupDomainService;
            this.userDomainService = userDomainService;
            this.roleDomainService = roleDomainService;
        }

        public StudentSelectModel GetStudentsListByStudyGroupId(int studyGroupId, UrlModel model)
        {
            IList<User> users = studyGroupDomainService.Get(studyGroupId).Students;
            return new StudentSelectModel(users, model, studyGroupId);
        }

        public StudentEditModel CreateStudentEditModel(int groupId)
        {
            StudentEditModel model = new StudentEditModel(groupId);
            FillEditModel(model);
            return model;
        }

        public StudentPartialEditModel CreateStudentPartialEditModel(int groupId)
        {
            StudentPartialEditModel model = new StudentPartialEditModel(groupId);
            return model;
        }

        public StudentEditModel GetStudentEditModel(int studentId)
        {
            User user = userDomainService.Get(studentId);
            StudentEditModel model = new StudentEditModel(user);
            FillEditModel(model);
            return model;
        }

        public StudentPartialEditModel GetStudentPartialEditModel(int studentId)
        {
            User user = userDomainService.Get(studentId);
            StudentPartialEditModel model = new StudentPartialEditModel(user);
            return model;
        }

        public void Delete(int studentId, int groupId)
        {
            var group = studyGroupDomainService.Get(groupId);
            studyGroupDomainService.DeleteStudent(group, studentId);
        }

        public void Save(StudentEditModel model)
        {
            User user = model.Id == 0 ? new User() : userDomainService.Get(model.Id);
            model.MapTo(user);
            StudyGroup studyGroup = studyGroupDomainService.Get(model.GroupId);
            studyGroup.Students.Add(user);
            studyGroupDomainService.Save(studyGroup);
        }

        public void SavePartial(StudentPartialEditModel studentPartialEditModel)
        {
            User user = new User();
            studentPartialEditModel.MapTo(user);
            StudyGroup studyGroup = studyGroupDomainService.Get(studentPartialEditModel.GroupId);
            studyGroup.Students.Add(user);
            studyGroupDomainService.Save(studyGroup);
        }

        private void FillEditModel(StudentEditModel model)
        {
            model.GenderTypeses = new List<SelectListItem>() { 
                                                                new SelectListItem() { Text = GenderTypes.Male.ToString(), Value = ((int)GenderTypes.Male).ToString()},
                                                                new SelectListItem() { Text = GenderTypes.Women.ToString(), Value = ((int)GenderTypes.Women).ToString()}
                                                             };
        }
    }
}